#!/usr/bin/env python
# coding: utf-8

# In[79]:


from sklearn.metrics import roc_auc_score, roc_curve, confusion_matrix,accuracy_score,precision_score,recall_score,f1_score
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
import numpy as np
from collections import Counter
import pickle
import itertools
import matplotlib
import time
import matplotlib.pyplot as plt
#from fenci import cut_word
from sklearn.model_selection import RandomizedSearchCV
# from sklearn.externals import joblib
from sklearn.preprocessing import label_binarize
from sklearn.metrics import auc
from sklearn.linear_model import LogisticRegression
from scipy import interp
from sklearn import preprocessing
from sklearn.decomposition import PCA
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
import warnings

warnings.filterwarnings('ignore')


# In[80]:


def unpickle(file):
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict


# In[81]:


train=unpickle('cifar-100-python/train')
test=unpickle('cifar-100-python/test')

print(train.keys())

train[b'filenames'] = train[b'filenames'][:100]
train[b'batch_label'] = train[b'batch_label'][:100]
train[b'fine_labels'] = train[b'fine_labels'][:100]
train[b'coarse_labels'] = train[b'coarse_labels'][:100]
train[b'data'] = train[b'data'][:100]

test[b'filenames'] = test[b'filenames'][:100]
test[b'batch_label'] = test[b'batch_label'][:100]
test[b'fine_labels'] = test[b'fine_labels'][:100]
test[b'coarse_labels'] = test[b'coarse_labels'][:100]
test[b'data'] = test[b'data'][:100]


# In[82]:


# 归一化
x_train = train[b'data']
x_test = test[b'data']
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255.
x_test /= 255.


# In[83]:


x_train[0].shape


# In[84]:


print(train)


# In[ ]:





# In[85]:


# x_train = train[b'data']
# x_test = test[b'data']
# min_max_scaler = preprocessing.MinMaxScaler()
# x_train = min_max_scaler.fit_transform(x_train)
# x_test = min_max_scaler.transform(x_test)


# # 下面这些是初始尝试

# ### 降到200维的准确率 

# In[86]:


# 降到200维
pca=PCA(n_components=50)
x_train = pca.fit_transform(x_train)
x_test = pca.transform(x_test)


# In[87]:


x_train[0].shape


# In[88]:


"""
RF
"""
start_time = time.time()
rf_clf = RandomForestClassifier(max_depth=16,random_state=0,n_jobs=4,n_estimators=150)
rf_clf.fit(x_train,train[b'fine_labels'])
end_time = time.time()
print('Running time:{0}'.format(end_time-start_time))


# In[89]:


# 模型预测
pred_rf = rf_clf.predict(x_test)
# 准确率
rf_accu = accuracy_score(test[b'fine_labels'], pred_rf)
print("RF_ACC:", rf_accu)
# 混淆矩阵
rf_confusion_matrix_result = confusion_matrix(test[b'fine_labels'], pred_rf)


# In[ ]:




